local lvshi = fk.CreateSkill{
  name = "hanqing__lvshi",
  tags = { Skill.Compulsory },
}

Fk:loadTranslationTable{
  ["hanqing__lvshi"] = "吕史",
  [":hanqing__lvshi"] = "锁定技，当你不以此法使用牌结算后，将之置于你的武将牌上，称为“史”。出牌阶段开始时，你可以声明你本回合只能使用“史”。",

  ["#hanqing__lvshi-invoke"] = "吕史：是否令你本回合只能使用“史”？",
}

lvshi:addEffect(fk.AfterCardsMove, {
  derived_piles = "hanqing__lvshi",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(lvshi.name) then
      local ids = {}
      for _, move in ipairs(data) do
        if move.toArea == Card.DiscardPile and move.moveReason == fk.ReasonUse then
          local parent_event = player.room.logic:getCurrentEvent().parent
          if parent_event ~= nil then
            if parent_event.event == GameEvent.UseCard then
              local use = parent_event.data
              if use.from == player then
                parent_event:searchEvents(GameEvent.MoveCards, 1, function(e2)
                  if e2.parent and e2.parent.id == parent_event.id then
                    for _, move2 in ipairs(e2.data) do
                      if move2.moveReason == fk.ReasonUse and move2.toArea == Card.Processing then
                        for _, info in ipairs(move2.moveInfo) do
                          if info.fromSpecialName ~= lvshi.name then
                            table.insertIfNeed(ids, info.cardId)
                          end
                        end
                      end
                    end
                  end
                end)
              end
            end
          end
        end
      end
      ids = player.room.logic:moveCardsHoldingAreaCheck(ids)
      if #ids > 0 then
        event:setCostData(self, {cards = ids})
        return true
      end
    end
  end,
  on_use = function (self, event, target, player, data)
    player:addToPile(lvshi.name, event:getCostData(self).cards, true, lvshi.name, player)
  end,
})

lvshi:addEffect(fk.EventPhaseStart, {
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(lvshi.name) and player.phase == Player.Play and
      #player:getPile(lvshi.name) > 0
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    if room:askToSkillInvoke(player, {
      skill_name = lvshi.name,
      prompt = "#hanqing__lvshi-invoke",
    }) then
      room:setPlayerMark(player, "hanqing__lvshi_invoke-turn", 1)
    end
  end,
})

lvshi:addEffect("filter", {
  handly_cards = function (self, player)
    if player:getMark("hanqing__lvshi_invoke-turn") > 0 then
      return player:getPile(lvshi.name)
    end
  end,
})

lvshi:addEffect("prohibit", {
  prohibit_use = function(self, player, card)
    if player:getMark("hanqing__lvshi_invoke-turn") > 0 then
      local subcards = Card:getIdList(card)
      return #subcards == 0 or table.find(subcards, function(id)
        return not table.contains(player:getPile(lvshi.name), id)
      end)
    end
  end,
  prohibit_response = function(self, player, card)
    if player:getMark("hanqing__lvshi_invoke-turn") > 0 then
      return not card:isVirtual() and table.contains(player:getPile(lvshi.name), card.id)
    end
  end,
})

return lvshi
